package Controller1;
import Dao.paymentDao;
import Dao.staffDao;
import java.lang.Integer;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Logincontroller {
	

	
		@FXML
		private AnchorPane myPane;
		@FXML
		Button login_button=new Button();
		@FXML
		Button register_button;
		@FXML
		private TextField accountField;
		@FXML
		private PasswordField passwordField;
		@FXML
		private RadioButton servant;
		@FXML
		private RadioButton manager;
		@FXML
		public ToggleGroup tg;
		
		@FXML
		public void initialize(){
			tg = new ToggleGroup();
			servant.setToggleGroup(tg);
			manager.setToggleGroup(tg);
			servant.setSelected(true); 
			
			//BackgroundImage myBI= new BackgroundImage(new Image("/image/login_background.jpg",750,440,false,true),
			//		BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
			 //         BackgroundSize.DEFAULT);
			//myPane.setBackground(new Background(myBI));
		}
		public void login() throws Exception {
			
	        int id =Integer.parseInt(accountField.getText().trim());
	        String password = passwordField.getText().trim();
	       String level="";
	        if(servant.isSelected())
	        	level+="服务员";
	        //调用登录功能管理员
	        else if(manager.isSelected())
	        	level+="管理员";
	        //System.out.println(staffDao.Login(id, password));
	        if (staffDao.Login(id, password,level)) {
	            Alert alert = new Alert(Alert.AlertType.INFORMATION);
	            alert.setTitle("提示");
	            alert.setContentText("登录成功!");
	        
	            alert.showAndWait();
	            //创建主界面舞台
	           
	            Stage loginStage =(Stage)login_button.getScene().getWindow();
	            loginStage.close();
	            
	            
	            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("View1/MainView.fxml"));
	            MainViewController mainController = fxmlLoader.getController();
	            mainController.model.setText(accountField.getText());
	            Stage mainStage =(Stage)login_button.getScene().getWindow();//传递到主界面
	            //读入布局
	       
	            Parent root = fxmlLoader.load();
	            Scene scene = new Scene(root);
	            mainStage.setTitle("加州阳光POS系统");
	            //mainStage.setMaximized(true);
	            mainStage.setScene(scene);
	            mainStage.show();
	            
	        } else {
	            Alert alert = new Alert(Alert.AlertType.INFORMATION);
	            alert.setTitle("提示");
	            alert.setContentText("账号或密码错误，登录失败!");
	            alert.showAndWait();
	        }
	    }
		//public void mainWindow() throws Exception {
		//	Stage stage =(Stage)login_button.getScene().getWindow();
		//	stage.close();
		//	GoodsController good = new GoodsController();
		//	good.start(stage);
		//}
		public void start(Stage primaryStage) throws Exception{
	        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View1/Login.fxml"));
	        primaryStage.setScene(new Scene(root));
	        primaryStage.setResizable(false);
	        primaryStage.show();
	    }
		
		public void registerWindow() throws Exception {
			Stage stage = (Stage)accountField.getScene().getWindow();
			stage.close();
			RegisterController rg = new RegisterController();
			rg.start(stage);
		}
		

}
